package com.sgb.kjwl.viewmodel.shopping;

import android.annotation.SuppressLint;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.AddShoppingCar;
import com.sgb.kjwl.model.entity.DeleteShoppingCar;
import com.sgb.kjwl.model.entity.shopping.CartItemPriceEntity;
import com.sgb.kjwl.model.entity.shopping.ShoppingCarResponse;
import com.sgb.kjwl.repository.ShoppingCarRepertory;
import com.sgb.kjwl.view.ui.fragment.self.ShoppingCartFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author Administrator
 */
public class ShoppingCarModel extends BaseViewModel {
    private ShoppingCartFragment fragment;
    private ShoppingCarRepertory repertory;

    public ShoppingCarModel(ShoppingCartFragment fragment, ShoppingCarRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void getShoppingCarInfo(Boolean notify) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);

        NewAPIManager.getInstance().getShoppingCarNewOrderAPI().getShoppingCarEntity().enqueue(new Callback<BaseEntity<List<ShoppingCarResponse>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ShoppingCarResponse>>> call, Response<BaseEntity<List<ShoppingCarResponse>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (notify) {
                        fragment.notifyData(response.body().getData());
                    } else {
                        fragment.showShoppingCarDetail(response.body().getData());
                    }
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ShoppingCarResponse>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    @SuppressLint("CheckResult")
    @Deprecated
    public void addShoppingCar(AddShoppingCar addShoppingCar) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.addShoppingCar(addShoppingCar)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity shoppingCarEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        //fragment.showShoppingCarDetail(shoppingCarEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                    }
                });
    }

    /***
     * 根据购物车的id更新商品数量
     * @param id
     * @param itemNum
     */
    public void updateShoppingCar(String id, String itemNum) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String, String> map = new HashMap<>(2);
        map.put("id", id);
        map.put("itemNum", itemNum);

        APIManager.getInstance().getShoppingCarAPI().getCarCount(map).enqueue(new Callback<BaseEntity<Boolean>>() {
            @Override
            public void onResponse(Call<BaseEntity<Boolean>> call, Response<BaseEntity<Boolean>> response) {
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<Boolean>> call, Throwable throwable) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, throwable.getMessage());
            }
        });
    }

    @Deprecated
    public void checkCartItemPrice(List<CartItemPriceEntity> cartItemPriceEntities) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getShoppingCarAPI().checkCartItemPrice(cartItemPriceEntities).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                Map<String, Boolean> map = (Map) response.body().getData();
                fragment.setCheckResult(map.get("flag"));
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable throwable) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, throwable.getMessage());
            }
        });
    }

    /***
     * 根据购物车的id删除购物车商品
     * @param deleteIds
     */
    public void deleteShoppingCar(List<String> deleteIds) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);

        NewAPIManager.getInstance().getShoppingCarNewOrderAPI().deleteShoppingCar(deleteIds).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    getShoppingCarInfo(true);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /***
     * 根据用户id删除清空购物车无效商品
     * @param
     */
    public void deleteInvalidationShoppingCar() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getShoppingCarNewOrderAPI().deleteInvalidationShoppingCar().enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    getShoppingCarInfo(true);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


}
